//
//  WeMyAuctionCell.m
//  WeGalaxy
//
//  Created by Zheng on 2017/7/7.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "WeMyAuctionCell.h"

@implementation WeMyAuctionCellFunctionView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.layer.cornerRadius = 5;
        self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        self.layer.borderWidth = 1;
        self.layer.masksToBounds = YES;

        [self addSubview:self.outBtn];
        [self addSubview:self.offerBtn];
        
        [_outBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.mas_equalTo(30);
        }];
        
        [_offerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self);
            make.top.equalTo(_outBtn.mas_bottom);
        }];
    }
    return self;
}

- (UIButton *)outBtn {
    if (!_outBtn) {
        _outBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _outBtn.backgroundColor = [UIColor whiteColor];
        [_outBtn setTitle:@"出局" forState:UIControlStateNormal];
        [_outBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _outBtn.titleLabel.font = kCX_FONT(14);
    }
    return _outBtn;
}

- (UIButton *)offerBtn {
    if (!_offerBtn) {
        _offerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _offerBtn.backgroundColor = kRGB_COLOR(240, 68, 52, 1);
        [_offerBtn setTitle:@"出价" forState:UIControlStateNormal];
        _offerBtn.titleLabel.font = kCX_FONT(14);
    }
    return _offerBtn;
}

@end

@interface WeMyAuctionCell ()

@property (nonatomic) dispatch_source_t timer;
@property (nonatomic, strong) WeMyAuctionCellFunctionView *functionView;
@property (nonatomic, copy) void(^outClickBlock)(NSIndexPath *indexPath);
@property (nonatomic, copy) void(^offerClickBlock)(NSIndexPath *indexPath);
@property (nonatomic, strong) NSIndexPath *outIndexPath;
@property (nonatomic, strong) NSIndexPath *offerIndexPath;

@end

@implementation WeMyAuctionCell

#pragma mark - 接口
- (void)clickOutBtnWithIndexPath:(NSIndexPath *)indexPath withClickBlock:(void (^)(NSIndexPath *))block {
    _outClickBlock = block;
    _outIndexPath = indexPath;
}

- (void)clickOfferBtnWithIndexPath:(NSIndexPath *)indexPath withClickBlock:(void (^)(NSIndexPath *))block {
    _offerClickBlock = block;
    _offerIndexPath = indexPath;
}

#pragma mark - 点击事件
- (void)btnClickAction:(UIButton *)sender {
    if (sender == _functionView.outBtn) {
        _outClickBlock(_outIndexPath);
    } else {
        _offerClickBlock(_offerIndexPath);
    }
}

#pragma mark - 传值
- (void)setType:(WeMyAuctionTopSelectType)type {
    _type = type;

    switch (_type) {
        case WeMyAuctionTopSelectTypeCompete: {
            [self timeCountDownWithSecond:1800];
            _functionView.hidden = NO;
        }
            break;
        case WeMyAuctionTopSelectTypeFinish: {
            dispatch_source_cancel(_timer);
            _timeLbl.text = @"已结束";
            _functionView.hidden = YES;
        }
            break;
        case WeMyAuctionTopSelectTypeOrder: {
            dispatch_source_cancel(_timer);
            _timeLbl.text = @"交易关闭";
            _functionView.hidden = YES;
        }
            break;
    }
    
}


#pragma mark - 倒计时
- (void)timeCountDownWithSecond:(NSInteger)time {
    
    if (_timer) {
        dispatch_source_cancel(_timer);
    }
    
    __block NSInteger timeout = time;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    _timer = timer;
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        if(timeout < 0){
            dispatch_source_cancel(timer);
        } else {
            NSInteger minute = timeout/60;
            NSString *minuteStr = @"";
            minuteStr = minute >= 10 ? [NSString stringWithFormat:@"%ld",minute]: [NSString stringWithFormat:@"0%ld",minute];
            
            NSString *secondStr = @"";
            NSInteger second = timeout%60;
            secondStr = second >= 10 ? [NSString stringWithFormat:@"%ld",second]: [NSString stringWithFormat:@"0%ld",second];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                _timeLbl.text = [NSString stringWithFormat:@"距离结束%@:%@",minuteStr,secondStr];
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}

#pragma mark - 子视图
- (void)subviewsInContentView {
    
    [self.contentView addSubview:self.timeLbl];
    [self.contentView addSubview:self.titleImgv];
    [self.contentView addSubview:self.titleLbl];
    [self.contentView addSubview:self.priceLbl];
    [self.contentView addSubview:self.marginLbl];
    [self.contentView addSubview:self.functionView];
    
    [_timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(40);
    }];
    
    [_titleImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLbl.mas_bottom).offset(6);
        make.left.equalTo(_timeLbl.mas_left).offset(7);
        make.width.mas_equalTo(95);
        make.height.mas_equalTo(85);
    }];
    
    [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLbl.mas_bottom).offset(7);
        make.left.equalTo(_titleImgv.mas_right).offset(15);
    }];
    
    [_priceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLbl);
        make.centerY.equalTo(_titleImgv);
    }];
    
    [_marginLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLbl);
        make.bottom.equalTo(_titleImgv).offset(-2);
    }];
    
    [_functionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_timeLbl).offset(-10);
        make.top.equalTo(_timeLbl.mas_bottom).offset(10);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(80);
    }];
    
}

- (UILabel *)marginLbl {
    if (!_marginLbl) {
        _marginLbl = [UILabel new];
        _marginLbl.font = kCX_FONT(12);
        _marginLbl.textColor = [UIColor lightGrayColor];
        _marginLbl.text = @"保证金  ¥ 5";
    }
    return _marginLbl;
}

- (UILabel *)priceLbl {
    if (!_priceLbl) {
        _priceLbl = [UILabel new];
        _priceLbl.font = kCX_FONT(12);
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"当前出价  ¥ 30"];
        NSRange range = NSMakeRange(6, [str length]-6);
        [str addAttribute:NSForegroundColorAttributeName value:kRGB_COLOR(240, 68, 52, 1) range:range];
        _priceLbl.attributedText = str;
    }
    return _priceLbl;
}

- (UILabel *)titleLbl {
    if (!_titleLbl) {
        _titleLbl = [UILabel new];
        _titleLbl.text = @"王石的广告";
        _titleLbl.font = kCX_FONT(15);
    }
    return _titleLbl;
}

- (UILabel *)timeLbl {
    if (!_timeLbl) {
        _timeLbl = [UILabel new];
        _timeLbl.font = kCX_FONT(14);
        _timeLbl.backgroundColor = kRGB_COLOR(240, 68, 52, 1);
        _timeLbl.textAlignment = NSTextAlignmentCenter;
        _timeLbl.textColor = [UIColor whiteColor];
    }
    return _timeLbl;
}

- (UIImageView *)titleImgv {
    if (!_titleImgv) {
        _titleImgv = [UIImageView new];
        _titleImgv.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _titleImgv.layer.borderWidth = 1;
        _titleImgv.layer.backgroundColor = [[UIColor orangeColor] CGColor];
    }
    return _titleImgv;
}

- (WeMyAuctionCellFunctionView *)functionView {
    if (!_functionView) {
        _functionView = [[WeMyAuctionCellFunctionView alloc] init];
        _functionView.hidden = YES;
        [_functionView.outBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_functionView.offerBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _functionView;
}

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self subviewsInContentView];
    }
    return self;
}

#pragma mark - 绘制
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGRect frame = (CGRect){10,10,kSCREEN_WIDTH-20,145};
    [[UIColor lightGrayColor] set];
    UIRectFrame(frame);
}

@end
